/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.cspimp.esrz.service.impl;

import me.zhengjie.modules.cspimp.esrz.domain.JhaItem;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.cspimp.esrz.repository.JhaItemRepository;
import me.zhengjie.modules.cspimp.esrz.service.JhaItemService;
import me.zhengjie.modules.cspimp.esrz.service.dto.JhaItemDto;
import me.zhengjie.modules.cspimp.esrz.service.dto.JhaItemQueryCriteria;
import me.zhengjie.modules.cspimp.esrz.service.mapstruct.JhaItemMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://docs.auauz.net
* @description 服务实现
* @author 黄星星
* @date 2020-06-26
**/
@Service
@RequiredArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class JhaItemServiceImpl implements JhaItemService {

    private final JhaItemRepository jhaItemRepository;
    private final JhaItemMapper jhaItemMapper;

    @Override
    public Map<String,Object> queryAll(JhaItemQueryCriteria criteria, Pageable pageable){
        Page<JhaItem> page = jhaItemRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(jhaItemMapper::toDto));
    }

    @Override
    public List<JhaItemDto> queryAll(JhaItemQueryCriteria criteria){
        return jhaItemMapper.toDto(jhaItemRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    public JhaItemDto findById(Long id) {
        JhaItem jhaItem = jhaItemRepository.findById(id).orElseGet(JhaItem::new);
        ValidationUtil.isNull(jhaItem.getId(),"JhaItem","id",id);
        return jhaItemMapper.toDto(jhaItem);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public JhaItemDto create(JhaItem resources) {
        return jhaItemMapper.toDto(jhaItemRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(JhaItem resources) {
        JhaItem jhaItem = jhaItemRepository.findById(resources.getId()).orElseGet(JhaItem::new);
        ValidationUtil.isNull( jhaItem.getId(),"JhaItem","id",resources.getId());
        jhaItem.copy(resources);
        jhaItemRepository.save(jhaItem);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            jhaItemRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<JhaItemDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (JhaItemDto jhaItem : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("所属公司", jhaItem.getCid());
            map.put("所属项目", jhaItem.getPid());
            map.put("创建时间", jhaItem.getCreateTime());
            map.put("更新时间", jhaItem.getUpdateTime());
            map.put("创建者", jhaItem.getCreateBy());
            map.put("更新者", jhaItem.getUpdateBy());
            map.put("名称", jhaItem.getItemName());
            map.put("危害或者潜在事件", jhaItem.getHazardOrPotentialEvent());
            map.put("产生偏差的主要后果", jhaItem.getMainConsequencesDeviations());
            map.put("现有安全控制措施", jhaItem.getExistingSafetyControlMeasures());
            map.put("建议改正/控制措施", jhaItem.getControlMeasures());
            map.put("分析方法", jhaItem.getAnalyticalMethod());
            map.put("事件发生可能性主键", jhaItem.getItemHappenId());
            map.put("事件发生可能性名称", jhaItem.getItemHappenName());
            map.put("暴露于危险环境的频繁程度主键", jhaItem.getItemEId());
            map.put("暴露于危险环境的频繁程度名称", jhaItem.getItemEName());
            map.put("发生事故事件偏差产生的后果严重性主键", jhaItem.getItemCId());
            map.put("发生事故事件偏差产生的后果严重性名称", jhaItem.getItemCName());
            map.put("R（风险值=L*E*C", jhaItem.getItemR());
            map.put("风险等级主键", jhaItem.getItemRiskId());
            map.put("风险等级code", jhaItem.getItemRiskCode());
            map.put("风险等级名称", jhaItem.getItemRiskName());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}